﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LowLevelGraphics;
using LowLevelGraphics.Filter;

namespace LowLevelGraphics.Filter
{
    //s. halcon "emphasize"
    public class Emphasize : BaseImageFilter
    {
        protected MeanFilter mean = new MeanFilter();
        public Emphasize()
        {
        }

        internal Emphasize(Emphasize _emphasize)
        {
        }

        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            //res := round((orig - mean) * Factor) + orig
            UnsafeBitmap bitmapCloned = (UnsafeBitmap)_bitmap.Clone();
            mean.Execute(bitmapCloned);

            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            float factor = 1.1f;
            int orig = 22;
            int r = 0;
            for (int y = 0; y < nHeight; y++)
            {
                for (int x = 0; x < nWidth; x++)
                {
                    r = (int)(
                        (_bitmap.GetPixel(x, y).R - bitmapCloned.GetPixel(x, y).R) * factor + orig
                    );
                    r = ImageMath.Clamp(r, 0, 255);
                    _bitmap.SetGrayPixel(x, y, r);
                }
            }
            return _bitmap;
        }

        public override object Clone()
        {
            return new Emphasize(this);
        }
    }
}
